package com.cqjtu.eecs.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqjtu.eecs.entity.Teacher;
import com.cqjtu.eecs.entity.dto.TeacherDto;
import com.cqjtu.eecs.entity.params.teacher.TeacherAddParams;
import com.cqjtu.eecs.entity.params.teacher.TeacherUpdateParams;
import com.cqjtu.eecs.entity.vo.TeacherCodeNameVo;
import com.cqjtu.eecs.entity.vo.TeacherVo;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.ITeacherService;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultCode;
import com.cqjtu.eecs.utils.listener.TeacherListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 教师 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/teacher")
@Api(tags = "教师管理接口")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @PostMapping("/upload")
    @ApiOperation("教师信息 excel批量导入")
    @Transactional(rollbackFor = Exception.class)
    public Result uploadTeacherExcelInfo(@RequestBody MultipartFile file) {
        if (Objects.isNull(file))
            return Result.error(ResultCode.PARAMETER_ERROR);
        try {
            EasyExcel.read(file.getInputStream(),
                    TeacherDto.class, new TeacherListener(teacherService))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error();
        }
        return Result.success();
    }

    @PostMapping("/add")
    @ApiOperation("添加一条教师信息")
    public Result addATeacherInfo(@Valid @RequestBody TeacherAddParams teacherAddParams) {
        return teacherService.addATeacherInfo(teacherAddParams);
    }

    @GetMapping("/teacher-code-name/{majorCode}")
    @ApiOperation(value = "根据专业代码查询该学院所有教师工号姓名")
    public List<TeacherCodeNameVo> getTeacherCodeNameVoByMajorCode(@PathVariable String majorCode) {
        return teacherService.getTeacherCodeNameVoByMajorCode(majorCode);
    }

    @GetMapping("/all-teacher-info/{current}/{size}")
    @ApiOperation(value = "分页获取全部教师信息", notes = "current-当前第几页，size-每页大小")
    public Page<TeacherVo> getAllTeacherInfo(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        return teacherService.getAllTeacherInfo(current, size, null);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改教师信息")
    public Result updateTeacherInfo(@Valid @RequestBody TeacherUpdateParams teacherUpdateParams) {
        return teacherService.updateTeacherInfo(teacherUpdateParams);
    }

    @GetMapping("/exist/{teacherCode}")
    @ApiOperation(value = "根据教师工号判断教师是否存在")
    public Teacher getTeacherByTeacherCode(@PathVariable String teacherCode) {
        Assert.notBlank(teacherCode, new ParamsErrorException("教师工号不能为空"));
        return teacherService.getOne(new QueryWrapper<Teacher>().eq("teacher_code", teacherCode));
    }

    @GetMapping("/all-teacherInfo/academy/{current}/{size}/{academyName}")
    @ApiOperation(value = "分页获取某学院下全部教师的信息", notes = "current-当前第几页，size-每页大小，academyName-学院名")
    public Page<TeacherVo> getAllTeacherInfo(@PathVariable Long current, @PathVariable Long size, @PathVariable String academyName) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        Assert.notBlank(academyName, new ParamsErrorException("学院名不能为空"));
        return teacherService.getAllTeacherInfo(current, size, academyName);
    }

    @PutMapping("/dimmision/{teacherCode}")
    @ApiOperation(value = "教师离职")
    public Result teacherDimmison(@PathVariable String teacherCode) {
        Assert.notBlank(teacherCode, new ParamsErrorException("教师工号不能为空"));
        boolean update = teacherService.update(new UpdateWrapper<Teacher>().eq("teacher_code", teacherCode).set("dimmision", true));
        if (update)
            return Result.success();
        return Result.error();
    }
}
